﻿using Blog.Core.Model.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Model.Seed
{
    public class DBSeed
    {
        /// <summary>
        /// 异步添加种子数据
        /// </summary>
        /// <param name="myContext"></param>
        /// <returns></returns>
        public static async Task SeedAsync(MyContext myContext)
        {
            try
            {
                // 创建数据库
                myContext.Db.DbMaintenance.CreateDatabase();

                // 注意！一定要先手动创建一个空的数据库，5.x 版本会自动创建数据库了
                // 会覆盖，可以设置为true，来备份数据
                // 如果生成过了，第二次，就不用再执行一遍了,注释掉该方法即可
                myContext.CreateTableByEntity(false, typeof(Advertisement));
                myContext.CreateTableByEntity(false, typeof(Modules));
                myContext.CreateTableByEntity(false, typeof(Role));
                myContext.CreateTableByEntity(false, typeof(RoleModulePermission));

                // 下边的就是种子数据
                #region Advertisement
                if (!await myContext.Db.Queryable<Advertisement>().AnyAsync())
                {
                    myContext.GetEntityDB<Advertisement>().Insert(
                       new Advertisement()
                       {
                           Createdate = DateTime.Now,
                           Remark = "mark",
                           Title = "good"
                       });
                }
                #endregion

                #region BlogArticle Guestbook
                //if (!await myContext.Db.Queryable<BlogArticle>().AnyAsync())
                //{
                //    int bid = myContext.GetEntityDB<BlogArticle>().InsertReturnIdentity(
                //         new BlogArticle()
                //         {
                //             bsubmitter = "admins",
                //             btitle = "老张的哲学",
                //             bcategory = "技术博文",
                //             bcontent = "<p>1。。。。。。",
                //             btraffic = 1,
                //             bcommentNum = 0,
                //             bUpdateTime = DateTime.Now,
                //             bCreateTime = DateTime.Now
                //         });

                //    if (bid > 0)
                //    {

                //        if (!await myContext.Db.Queryable<Guestbook>().AnyAsync())
                //        {
                //            myContext.GetEntityDB<Guestbook>().Insert(
                //               new Guestbook()
                //               {
                //                   blogId = bid,
                //                   createdate = DateTime.Now,
                //                   username = "user",
                //                   phone = "110",
                //                   QQ = "100",
                //                   body = "很不错",
                //                   ip = "127.0.0.1",
                //                   isshow = true,
                //               });
                //        }
                //    }
                //}
                #endregion

                #region Modules
                int mid = 0;
                if (!await myContext.Db.Queryable<Modules>().AnyAsync())
                {
                    mid = myContext.GetEntityDB<Modules>().InsertReturnIdentity(
                        new Modules()
                        {
                            IsDeleted = false,
                            Name = "values的接口信息",
                            LinkUrl = "/api/values",
                            OrderSort = 1,
                            IsMenu = false,
                            Enabled = true
                        });

                }
                #endregion

                #region Role
                int rid = 0;
                if (!await myContext.Db.Queryable<Role>().AnyAsync())
                {
                    rid = myContext.GetEntityDB<Role>().InsertReturnIdentity(
                        new Role()
                        {
                            IsDeleted = false,
                            Name = "Admin",
                            Description = "我是一个admin管理员",
                            OrderSort = 1,
                            CreateTime = DateTime.Now,
                            Enabled = true,
                            ModifyTime = DateTime.Now
                        });

                }
                #endregion

                #region RoleModulePermission
                if (mid > 0 && rid > 0)
                {
                    if (!await myContext.Db.Queryable<RoleModulePermission>().AnyAsync())
                    {
                        myContext.GetEntityDB<RoleModulePermission>().Insert(
                            new RoleModulePermission()
                            {
                                IsDeleted = false,
                                RoleId = rid,
                                ModuleId = mid,
                                CreateTime = DateTime.Now,
                                ModifyTime = DateTime.Now
                            });
                    }
                }
                #endregion

                #region sysUserInfo
                //int uid = 0;
                //if (!await myContext.Db.Queryable<sysUserInfo>().AnyAsync())
                //{
                //    uid = myContext.GetEntityDB<sysUserInfo>().InsertReturnIdentity(
                //        new sysUserInfo()
                //        {
                //            uLoginName = "admins",
                //            uLoginPWD = "admins",
                //            uRealName = "admins",
                //            uStatus = 0,
                //            uCreateTime = DateTime.Now,
                //            uUpdateTime = DateTime.Now,
                //            uLastErrTime = DateTime.Now,
                //            uErrorCount = 0

                //        });

                //}
                #endregion

                #region UserRole
                //if (uid > 0 && rid > 0)
                //{
                //    if (!await myContext.Db.Queryable<UserRole>().AnyAsync())
                //    {
                //        myContext.GetEntityDB<UserRole>().Insert(
                //            new UserRole()
                //            {
                //                IsDeleted = false,
                //                UserId = uid,
                //                RoleId = rid,
                //                CreateTime = DateTime.Now,
                //                ModifyTime = DateTime.Now
                //            });
                //    }
                //}
                #endregion

                #region Topic TopicDetail
                //if (!await myContext.Db.Queryable<Topic>().AnyAsync())
                //{
                //    int tid = myContext.GetEntityDB<Topic>().InsertReturnIdentity(
                //         new Topic()
                //         {
                //             tLogo = "/Upload/20180626/95445c8e288e47e3af7a180b8a4cc0c7.jpg",
                //             tName = "《罗马人的故事》",
                //             tDetail = "这是一个荡气回肠的故事",
                //             tIsDelete = false,
                //             tRead = 0,
                //             tCommend = 0,
                //             tGood = 0,
                //             tCreatetime = DateTime.Now,
                //             tUpdatetime = DateTime.Now,
                //             tAuthor = "laozhang"
                //         });

                //    if (tid > 0)
                //    {

                //        if (!await myContext.Db.Queryable<TopicDetail>().AnyAsync())
                //        {
                //            myContext.GetEntityDB<TopicDetail>().Insert(
                //               new TopicDetail()
                //               {
                //                   TopicId = tid,
                //                   tdLogo = "/Upload/20180627/7548de20944c45d48a055111b5a6c1b9.jpg",
                //                   tdName = "第一章　罗马的诞生 第一节　传说的年代",
                //                   tdContent = "<p>第一节　传说的年代</时代走出，近入了历史时代。</p><p><br></p>",
                //                   tdDetail = "第一回",
                //                   tdIsDelete = false,
                //                   tdRead = 1,
                //                   tdCommend = 0,
                //                   tdGood = 0,
                //                   tdCreatetime = DateTime.Now,
                //                   tdUpdatetime = DateTime.Now,
                //                   tdTop = 0,
                //               });
                //        }
                //    }
                //}
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
